package com.l024.meet.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.l024.framework.bmob.BmobManager;
import com.l024.framework.entity.Constants;
import com.l024.framework.utils.SpUtils;
import com.l024.meet.MainActivity;
import com.l024.meet.R;

/**
 * 启动页
 */
public class IndexActivity extends AppCompatActivity {

    /**
     * 1.启动全屏
     * 2.延迟进入主页
     * 3.根据具体逻辑判断进入主页还是引导页还是登录页
     * 4.适配刘海屏
     * @param savedInstanceState
     */

    private static final int SKIP_MAIN = 1000;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case SKIP_MAIN:{
                    startMain();
                    break;
                }
            }
            return false;
        }
    });
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        handler.sendEmptyMessageDelayed(SKIP_MAIN,2*1000);
    }

    /**
     * 进入主页判断
     */
    private void startMain(){
        //1.判断是否第一次进入app 安装到打开app
        boolean isFirstApp = SpUtils.getInstance().getBoolean(Constants.SP_IS_FIRST_APP, true);
        Intent intent = new Intent();
        if(isFirstApp){
            intent.setClass(IndexActivity.this,GuideActivity.class);
            SpUtils.getInstance().putBoolean(Constants.SP_IS_FIRST_APP,false);
        }else{
            //2.判断是否登录过，根据token判断
            String token = SpUtils.getInstance().getString(Constants.SP_TOKEN, "");
            if(TextUtils.isEmpty(token)){
                //判断是否登录过
                if(BmobManager.getInstance().isLogin()){
                    intent.setClass(IndexActivity.this, MainActivity.class);
                }else{
                    //跳转登录页
                    intent.setClass(IndexActivity.this,LoginActivity.class);
                }
            }else{
                //跳转首页
                intent.setClass(IndexActivity.this, MainActivity.class);
//                intent.setClass(IndexActivity.this, TestActivity.class);
            }
        }
        startActivity(intent);
        finish();
    }
}
